﻿#include "Jar1.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../DelayedAction.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    unsigned char Jar1::getAttackRange() const
    {
        return 1;
    }

    bool Jar1::getCanMove() const
    {
        return false;
    }

    bool Jar1::getCanRegen() const
    {
        return false;
    }

    bool Jar1::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), getAttackRange());
    }

    Jar1::Jar1(MonsterInfo *info) : MonsterObject(info)
    {

    }

    void Jar1::Die()
    {
        DelayedAction tempVar(DelayedType::Die, getEnvir()->getTime() + 1000);
        ActionList.push_back(&tempVar);

        MonsterObject::Die();
    }

    void Jar1::CompleteDeath(std::vector<std::any> &data)
    {
        SpawnSlave();
    }

    void Jar1::SpawnSlave()
    {
        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        auto monsters = getEnvir()->MonsterInfoList.Where([&] (std::any x)
        {
            return x::Level <= getLevel() && x::Level >= (getLevel() - 10);
        });

        if (monsters->Count() > 0)
        {
            auto idx = getEnvir()->Random->Next(monsters->Count());

            auto monster = monsters->ElementAt(idx);

            auto mob = GetMonster(monster);

            if (mob == nullptr)
            {
                return;
            }

            mob->Spawn(getCurrentMap(), getCurrentLocation());

            mob->setTarget(getTarget());
            mob->ActionTime = getEnvir()->getTime() + 2000;
        }
    }
}
